﻿using System;
using System.Threading;
using System.Data.Linq;
using System.Transactions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plat.WfProcess.Common;
using Plat.WfProcess.DataModel;
using Plat.WfProcess.Business;
using Plat.WfProcess.Utility;

namespace Plat.WfProcess.Engine
{
    /// <summary>
    /// 流程运行时管理
    /// </summary>
    public class WfRuntimeManager
    {
        #region WfRuntimeManager 属性、构造函数
        private static bool _IsStarted = false;
        private static long _WfRuntimeManagerCount = 0;

        public bool IsStarted
        {
            get
            {
                return _IsStarted;
            }
        }

        public WfRuntimeManager()
        {
            _WfRuntimeManagerCount++;
        }
        #endregion

        /// <summary>
        /// 启动流程运行时管理服务
        /// </summary>
        public void StartWorkflowRuntime()
        {
            //CreateMemoryClient();
            _IsStarted = true;
        }

        public long GetWfRuntimeManagerCount()
        {
            return _WfRuntimeManagerCount;
        }

        #region WfRuntimeManager 创建执行实例的运行者对象
        /// <summary>
        /// 创建流程执行时实例
        /// </summary>
        /// <param name="applicationInstanceID"></param>
        /// <param name="processGUID"></param>
        /// <param name="logonUser"></param>
        /// <param name="nextActivityPerformers"></param>
        /// <returns></returns>
        public WfRunnerInstance CreateWfRunnerInstance(long applicationInstanceID,
            string processGUID,
            ActivityResource activityResource)
        {
            WfRunnerInstance wfRunnerInstance = new WfRunnerInstance(applicationInstanceID, 
                processGUID, 
                activityResource);
            wfRunnerInstance.WfRuntimeManager = this;
            return wfRunnerInstance;
        }

        /// <summary>
        /// 创建流程执行时实例
        /// </summary>
        /// <param name="taskID">任务ID</param>
        /// <param name="currentLogonUser">当前登录用户</param>
        /// <param name="nextActivityPerformers">下一步活动的执行者</param>
        /// <param name="conditionKeyValuePair">条件key-value对</param>
        /// <returns></returns>
        public WfRunnerInstance CreateWfRunnerInstance(long taskID,
            ActivityResource activityResource)
        {
            WfRunnerInstance wfRunnerInstance = new WfRunnerInstance(taskID, activityResource);

            wfRunnerInstance.WfRuntimeManager = this;
            return wfRunnerInstance;
        }
        #endregion

        /// <summary>
        /// 创建MemoryCache的客户端
        /// </summary>
        private void CreateMemoryClient()
        {
            //BeITMemCacheHelper.BuildMemoryClient();
        }
    }
}
